package cn.wolfcode.web.util;

import cn.wolfcode.web.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.security.Permission;
import java.util.List;

public abstract class UserContext {//设置为抽象类,别人不能new对象
 /*   1.全员变量2.获取session对象
    3.设置公共静态set办法,调用方法者传参数,*/

//    提供公共静态get返回session对象和返回员工对象(密码和账号)
//流程:抽到常量
    public static final String EMPLOYEE_IN_SESSION="EMPLOYEE_IN_SESSION";
    public static final String  PERMISSIONS_IN_SESSION="EMPLOYEE_IN_SESSION";



    public static HttpSession getSession() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        return attrs.getRequest().getSession();//获取request对象再获取session对象返回
    }

    // 存入登录账号的员工对象在session对象   给调用者调用方法给参数,存入session
    public static void setCurrentUser(Employee employee) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION, employee);

    }
//调用方法获取存在session对象里面的该员工对象
    public static Employee getCurrentUser() {

        return   (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    //存入登录账号的员工对象的所有的权限表达式字符串存在session对象 给调用者调用方法给参数,存入session
    public static void setPermissions(List<String> permissions) {
        getSession().setAttribute(PERMISSIONS_IN_SESSION, permissions);

    }

    //调用方法获取存在session对象里面的该员工的所有权限
    public static List<String> getPermissions() {
//Object转换为List集合类型
        return (List<String>)getSession().getAttribute(PERMISSIONS_IN_SESSION);
    }

}
